<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title>Shop Management System</title>
<?php include('menu.php'); ?>
<?php
	if(isset($_SESSION['user_group_id'])){
		if($_SESSION['user_group_id']!=1) {
		echo "<script>window.location = '../index.php';</script>";
   		exit();
		}
	}
?>
</head>
<body>
<?php
include("../connect/connect.php");

	$Query = "SELECT * FROM debtor WHERE 1 ";

$sortcost = $_GET["sortcost"];
$sortqty = $_GET["sortqty"];
$sortcode = $_GET["sortcode"];
$sortname = $_GET["sortname"];
$sortrop = $_GET["sortrop"];
$sortcate = $_GET["sortcate"];
$sortctname = $_GET["sortctname"];

if(isset($_GET["sortcate"])){

	if($_GET["sortcate"]=="10"){
	$Query .= "";
	} else {
	$Query .= " AND deb_status = $sortcate ";
	}
}

	
if(isset($_GET["sortcode"])){
	$Query .= "ORDER BY deb_no $sortcode";
}
	
if(isset($_GET["sortcost"])){
	$Query .= "ORDER BY deb_amount $sortcost";
}

if(isset($_GET["sortqty"])){
	$Query .= "ORDER BY deb_status $sortqty";
}

if(isset($_GET["sortname"])){
	$Query .= "ORDER BY deb_date $sortname";
}

if(isset($_GET["sortctname"])){
	$Query .= "ORDER BY deb_name $sortctname";
}

$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>รายงานลูกหนี้</h1>
	</div>
  <div class="content">
    <div class="buttons" align="right"><b>แสดงข้อมูลตามสถานะ : </b>
      <select name="category_id" id="category_id" onchange="location.href=this.options[this.selectedIndex].value">
        <option <?php if($sortcate=="10") {echo "selected";} ?> value="?sortcate=10<?php if(isset($sortcost)){echo "&sortcost=$sortcost";} ?><?php if(isset($sortqty)){echo "&sortqty=$sortqty";} ?><?php if(isset($sortcode)){echo "&sortcode=$sortcode";} ?><?php if(isset($sortname)){echo "&sortname=$sortname";} ?><?php if(isset($sortrop)){echo "&sortrop=$sortrop";} ?><?php if(isset($sortctname)){echo "&sortctname=$sortctname";} ?>">ทั้งหมด</option>
        <option <?php if($sortcate=="1") {echo "selected";} ?> value="?sortcate=1<?php if(isset($sortcost)){echo "&sortcost=$sortcost";} ?><?php if(isset($sortqty)){echo "&sortqty=$sortqty";} ?><?php if(isset($sortcode)){echo "&sortcode=$sortcode";} ?><?php if(isset($sortname)){echo "&sortname=$sortname";} ?><?php if(isset($sortrop)){echo "&sortrop=$sortrop";} ?><?php if(isset($sortctname)){echo "&sortctname=$sortctname";} ?>">ชำระแล้ว</option>
        <option <?php if($sortcate=="0") {echo "selected";} ?> value="?sortcate=0<?php if(isset($sortcost)){echo "&sortcost=$sortcost";} ?><?php if(isset($sortqty)){echo "&sortqty=$sortqty";} ?><?php if(isset($sortcode)){echo "&sortcode=$sortcode";} ?><?php if(isset($sortname)){echo "&sortname=$sortname";} ?><?php if(isset($sortrop)){echo "&sortrop=$sortrop";} ?><?php if(isset($sortctname)){echo "&sortctname=$sortctname";} ?>">ค้างชำระ</option>

      </select></div><br />
    <table class="list">
        <thead>
          <tr>
            <td width="142" style="text-align: center;"><span class="center"><?php if($sortcode=='asc'){ ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcode=desc">รหัสใบแจ้งหนี้ <img src="image/desc.png" width="13" height="13" border="0" /></a>
			<?php } else if($sortcode=='desc') { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcode=asc">รหัสใบแจ้งหนี้ <img src="image/asc.png" width="13" height="13" border="0" /></a>
			<?php } else { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcode=asc">รหัสใบแจ้งหนี้</a>
			<?php } ?></span></td>
            <td width="194" class="center"><?php if($sortname=='asc'){ ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortname=desc">วันที่ <img src="image/desc.png" width="13" height="13" border="0" /></a>
			<?php } else if($sortname=='desc') { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortname=asc">วันที่ <img src="image/asc.png" width="13" height="13" border="0" /></a>
			<?php } else { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortname=asc">วันที่</a>
			<?php } ?></td>
            <td width="335" class="center"><?php if($sortctname=='asc'){ ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortctname=desc">ชื่อลูกค้า <img src="image/desc.png" width="13" height="13" border="0" /></a>
			<?php } else if($sortctname=='desc') { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortctname=asc">ชื่อลูกค้า <img src="image/asc.png" width="13" height="13" border="0" /></a>
			<?php } else { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortctname=asc">ชื่อลูกค้า</a>
			<?php } ?></td>
            <td width="154" class="center"><?php if($sortcost=='asc'){ ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcost=desc">ยอดเงิน <img src="image/desc.png" width="13" height="13" border="0" /></a>
			<?php } else if($sortcost=='desc') { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcost=asc">ยอดเงิน <img src="image/asc.png" width="13" height="13" border="0" /></a>
			<?php } else { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcost=asc">ยอดเงิน</a>
			<?php } ?></td>
            <td width="154" class="center"><?php if($sortqty=='asc'){ ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortqty=desc">สถานะ <img src="image/desc.png" width="13" height="13" border="0" /></a>
			<?php } else if($sortqty=='desc') { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortqty=asc">สถานะ <img src="image/asc.png" width="13" height="13" border="0" /></a>
			<?php } else { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortqty=asc">สถานะ</a>
			<?php } ?></td>
          </tr>
        </thead>
        <tbody>
<?php
	$strnum=0;
	while($row = mysql_fetch_array($objQuery))
		{
		$strnum++;
?>
          <tr>
            <td style="text-align: center;"><?php echo $row["deb_no"];?></td>
            <td class="center"><?php echo $row["deb_date"];?></td>
            <td class="left" style="padding-left:30px"><?php echo $row["deb_name"];?></td>
            <td class="right" style="padding-right:30px"><?php echo $row["deb_amount"];?></td>
            <td class="center"><?php if($row["deb_status"]=='0'){echo "ค้างชำระ";} if($row["deb_status"]=='1'){echo "ชำระแล้ว";}?></td>
          </tr>
<?php
		}
		if($strnum==0) {
?>
        <tr>
          <td class="center" colspan="6">ไม่มีรายการ</td>
        </tr>
<?php
		}
?>
        </tbody>
    </table>
  </div>
</div>

</body>
</html>
